package com.xique.park.controller.client.web.service.biz.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xique.common.core.constant.HttpStatus;
import com.xique.common.core.domain.R;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.page.TableDataInfo;
import com.xique.park.bean.domain.ParkOn;
import com.xique.park.bean.vo.client.ClientParkOnRecordVo;
import com.xique.park.controller.client.web.bean.request.ClientParkOnListRequest;
import com.xique.park.controller.client.web.bean.request.WebClientInOutRecordRequest;
import com.xique.park.controller.client.web.bean.request.WebClientMultCarListRequest;
import com.xique.park.controller.client.web.service.biz.service.IClientRecordBizService;
import com.xique.park.service.biz.service.IClientBizService;
import com.xique.system.api.RemoteSystemDictService;
import com.xique.system.api.RemoteSystemUserService;
import com.xique.system.api.dto.OperateDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author shd
 * @create 2021/7/29 9:13
 */
@Service
public class ClientRecordBizServiceImpl implements IClientRecordBizService {

    @Autowired
    private IClientBizService clientBizService;

    @Autowired
    private RemoteSystemUserService remoteSystemUserService;

    @Autowired
    private RemoteSystemDictService dictService;

    /**
     * 在场列表
     *
     * @param request /
     * @return com.xique.common.core.web.domain.AjaxResult
     * @author shd
     * @date 2021/7/29 9:14
     */
    @Override
    public AjaxResult getParkOnList(ClientParkOnListRequest request) {
        request.setParkId(SecurityUtils.getParkId());
        String orderByColumn = "pko.in_time";
        String isAsc = "desc";
        if (StringUtils.isEmpty(orderByColumn) || StringUtils.isEmpty(isAsc)) {
            orderByColumn = "id";
            isAsc = "desc";
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), StringUtils.toUnderScoreCase(orderByColumn) + " " + isAsc);

        List<ParkOn> clientParkOnList = clientBizService.getClientParkOnList(request);

        if (CollectionUtil.isNotEmpty(clientParkOnList)) {
            StringBuilder updateUserIds = new StringBuilder();
            clientParkOnList.forEach(parkOn -> {
                Long updateUserId = parkOn.getUpdateUser();
                if (null == updateUserId) {
                    updateUserId = 1L;
                }
                updateUserIds.append(updateUserId).append(",");

                parkOn.setInStatusLabel(dictService.getDictLabel("car_pass_status", parkOn.getInStatus().toString()).getData());
                parkOn.setCarTypeLabel(dictService.getDictLabel("car_type", parkOn.getCarType()).getData());
                parkOn.setIsUpdate("N".equals(parkOn.getIsUpdate()) ? "否" : "是");
            });

            R<List<OperateDTO>> operateResult = remoteSystemUserService.getOperateList(updateUserIds.toString());
            List<OperateDTO> operateDTOList = operateResult.getData();
            if (CollectionUtil.isNotEmpty(operateDTOList)) {
                for (int i = 0; i < operateDTOList.size(); i++) {
                    clientParkOnList.get(i).setUpdateUserName(operateDTOList.get(i).getOperateName());
                }
            }
        }

        TableDataInfo dataTable = getDataTable(clientParkOnList);
        List<ClientParkOnRecordVo> clientParkOnRecordVos = clientBizService.convetParkOnVoList(clientParkOnList);


        dataTable.setRows(clientParkOnRecordVos);

        return new AjaxResult(dataTable);
    }

    private TableDataInfo getDataTable(List<?> list) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setRows(list);
        rspData.setMsg("查询成功");
        rspData.setTotal(new PageInfo<>(list).getTotal());
        return rspData;
    }

    @Override
    public AjaxResult getMultCarList(WebClientMultCarListRequest request) {
        return null;
    }

    @Override
    public AjaxResult getclientInOnList(WebClientInOutRecordRequest request) {
        return null;
    }
}
